yjx.controller('editPlanProduct', ['$scope', '$timeout', '$fetch', 'modalService', '$state', function ($scope, $timeout, $fetch, modalService, $state) {

    $scope.plan = $scope.parent.plan;
    $scope.producRectList = [].concat($scope.parent.producRectList);
    $scope.totalPrice = $scope.parent.totalPrice ? $scope.parent.totalPrice : 0;
    $scope.discountPrice = $scope.parent.discountPrice ? $scope.parent.discountPrice : 0;
    $scope.scrollLeft = 80;
    /*解决android中软键盘弹出 将页面压缩的问题*/
    var HEIGHT = $('body').height();
    $('.m-clientInfo').height(HEIGHT)
    /**保存 */
    $scope.submit = function () {
        if (isNaN($scope.discountPrice)) {
            modalService.mainMsgError("价格是数字形式");
            return;
        }
        if ($scope.discountPrice > $scope.totalPrice) {
            return;
            modalService.mainMsgError("优惠价格不能超过总价");
        }
        edit();
        // $scope.parent.plan.description =$('#description').html();
        // $scope.parent.producRectList.splice(0,$scope.parent.producRectList.length);
        // $scope.parent.producRectList= $scope.parent.producRectList.concat($scope.producRectList);
        // $scope.parent.totalPrice = $scope.totalPrice;
        // $scope.parent.discountPrice = $scope.discountPrice;
        // history.back();
        // $timeout(function(){
        //     $scope.$parent.resetHeight();
        // })
    }
    function edit() {
        var text = $('#description').html();
        $fetch.fetch(
            '/customer/prospectus',
            {
                params: {
                    customerId: $scope.$parent.customerId,
                    prospectus: text
                },
                type: 'POST'
            }
        ).then(function (data) {
            $scope.parent.plan.description = text;
            $scope.parent.producRectList.splice(0, $scope.parent.producRectList.length);
            $scope.parent.producRectList = $scope.parent.producRectList.concat($scope.producRectList);
            $scope.parent.totalPrice = $scope.totalPrice;
            $scope.parent.discountPrice = $scope.discountPrice;
            history.back();
            $timeout(function () {
                $scope.$parent.resetHeight();
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }

    /**添加推荐产品 */
    $scope.add = function () {
        $scope.selectTag = true;
        $timeout(function () {
            $state.go('clientDetail.editPlan.selectProduct')
        })
    }
    $scope.addProduct = function (item) {
        for (var i = 0; i < $scope.producRectList.length; i++) {
            if (item.id == $scope.producRectList[i].id) {
                history.back();
                return;
            }
        }
        $scope.producRectList.push(item);
        var price = item.price ? item.price : item.originalPrice;
        $scope.totalPrice = $scope.totalPrice + price;
        history.back();
    }
    $scope.delete = function (id) {
        for (var i = 0; i < $scope.producRectList.length; i++) {
            if ($scope.producRectList[i].id == id) {
                // $scope.$broadcast('checkitFalse',id);
                var price = $scope.producRectList[i].price ? $scope.producRectList[i].price : $scope.producRectList[i].originalPrice;
                $scope.totalPrice = $scope.totalPrice - price;
                $scope.producRectList.splice(i, 1);
                return;
            }
        }
    }
}])